package com.darren.euc.dao.impl;


import java.util.Comparator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.darren.euc.dao.PrintRecordDao;
import com.darren.euc.entity.PrintRecord;


@Repository
public class PrintRecordDaoImpl extends BaseDaoImpl<PrintRecord, Integer> implements PrintRecordDao {
	
	@SuppressWarnings("unchecked")
	@Override
	public List<PrintRecord> getAll() {
		Session session = sessionFactory.getCurrentSession();	
		List<PrintRecord> printRecords = session.createQuery(" from PrintRecord ").list();
		return printRecords;
	}
	
	@Override
	public PrintRecord getLastRecords(){
		Session session = sessionFactory.getCurrentSession();	
		List<PrintRecord> printRecords = session.createQuery(" from PrintRecord where jobInTime=(select max(jobInTime) from PrintRecord)  ").list();
		if (CollectionUtils.isEmpty(printRecords)){
			return null;
		}
		printRecords.sort(Comparator.comparing(PrintRecord::getId).reversed());
		return printRecords.get(0);
	}

	@Override
	public PrintRecord findByJobId(String jobId){
		Session session = sessionFactory.getCurrentSession();	
		Query query = session.createQuery(" from PrintRecord where jobId = :jobId  ");
		query.setParameter("jobId", jobId);
		PrintRecord printRecord = (PrintRecord) query.uniqueResult();
		return printRecord;
	}
	
	private String updateMemeberInfoSQL = "update euc_print_records as records  " +
										"			INNER JOIN euc_member as member on member.mobile = records.user_name  " +
										"			INNER JOIN euc_client as client on member.client_id = client.id  " +
										"			INNER JOIN euc_clues as clue on client.id = clue.client_id  " +
										"			INNER JOIN euc_spaces as space on space.id = clue.space_id  " +
										"	SET records.member_id = member.id,  " +
										"			records.member_name = member.`name`,  " +
										"			records.client_id = client.id,  " +
										"			records.client = if(client.client_type = '个人',client.member_name,client.company_name),  " +
										"			records.space_id = space.id,  " +
										"			records.space_name = space.space_name  " +
										"	WHERE member.employ_status <> -1  " +
										"	AND records.member_id IS NULL ;";	
												
	public void updateMemberInfo(){
		Session session = sessionFactory.getCurrentSession();
		Query query  = session.createSQLQuery(updateMemeberInfoSQL);
		query.executeUpdate();
		generateOrderNumber();
	}
	
	
	private String updateOrderNumber =  " UPDATE  euc_print_records as records  " +
										"	SET records.order_number = (select concat('P',DATE_FORMAT(records.job_in_time,'%Y%m%d'),LPAD(count(1),3,'0'))  from (SELECT * from euc_print_records) as intable where DATEDIFF(intable.job_in_time ,records.job_in_time) = 0 and intable.job_in_time <= records.job_in_time)   " +
										"	where records.member_id is not null  " +
										"	AND records.final_result is not null  " + 
										"	AND records.final_result = 1  " + 
										"	AND LENGTH(TRIM(IFNULL(records.order_number,''))) < 10;";	
	private void generateOrderNumber(){
		Session session = sessionFactory.getCurrentSession();
		Query query  = session.createSQLQuery(updateOrderNumber);
		query.executeUpdate();
	}
	
}